function getAllCookiesAsync(url) {
  return new Promise((resolve, reject) => {
    chrome.cookies.getAll({ url: url }, function (cookies) {
      resolve(cookies);
    });
  });
}

chrome.runtime.onMessage.addListener(async function (
  message,
  sender,
  sendResponse
) {
  if (message.action === "setCookie") {
    // 向 xueqiu.com 页面发送消息以设置本地存储值

    const tabs = await new Promise((resolve, reject) => {
      chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        resolve(tabs);
      });
    });

    if (tabs && tabs.length > 0) {
      getAllCookiesAsync(tabs[0].url).then((cookies) => {
        if (cookies && cookies.length) {
          console.log(cookies, "删除的cookies");
          for (var i = 0; i < cookies.length; i++) {
            chrome.cookies.remove({
              url: "http://" + cookies[i].domain + cookies[i].path,
              name: cookies[i].name,
            });
          }
        }
        if (message.cookieVal && message.cookieVal.length) {
          console.log(message.cookieVal, "set 的cookie");
          chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
            if (changeInfo.status === "complete") {
              chrome.tabs.sendMessage(
                tabId,
                {
                  action: "setLocalStorage",
                  key: "hp",
                  value: 1,
                },
                function (response) {
                  console.log(response.message);
                }
              );
            }
          });
          message.cookieVal.forEach((cookie) => {
            chrome.cookies.set(
              {
                url: "https://xueqiu.com/",
                name: cookie.name,
                value: cookie.value,
                domain: cookie.domain,
                path: cookie.path,
                secure: cookie.secure || false,
                httpOnly: cookie.httpOnly || false,
                expirationDate: cookie.expirationDate || 0,
              },
              function (cookie) {}
            );
          });
        } else {
          chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
            if (changeInfo.status === "complete") {
              chrome.tabs.sendMessage(
                tabId,
                {
                  action: "setLocalStorage",
                  key: "hp",
                  value: 2,
                },
                function (response) {
                  console.log(response.message);
                }
              );
            }
          });
        }

        chrome.tabs.query(
          { active: true, currentWindow: true },
          function (tabs) {
            if (tabs && tabs[0]) {
              setTimeout(() => {
                chrome.tabs.sendMessage(tabs[0].id, {
                  message: "1",
                });
              }, 500);
            }
          }
        );
      });
    }
  }
});

// 使用

// // 监听来自内容脚本的消息
// chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
//   if (message.action === "setCookie") {
//     chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
//       chrome.cookies.getAll({ url: tabs[0].url }, function (cookies) {
//         // 延迟执行for循环，等待console.log完成
//         setTimeout(function () {
//           console.log(cookies, "222222222222222");

//           for (var i = 0; i < document.cookie; i++) {
//             chrome.cookies.remove({
//               url: "http://" + cookies[i].domain + cookies[i].path,
//               name: cookies[i].name,
//             });
//           }
//         }, 2000); // 延迟1秒（1000毫秒），根据实际情况调整
//       });
//     });

//     // chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
//     //   chrome.tabs.sendMessage(tabs[0].id, { message: "3" });
//     // });
//     setTimeout(() => {
//       if (message.cookieVal && message.cookieVal.length) {
//         message.cookieVal.forEach((cookie) => {
//           console.log(cookie.name, "cookie.name");
//           chrome.cookies.set(
//             {
//               url: "https://xueqiu.com/",
//               name: cookie.name,
//               value: cookie.value,
//               domain: cookie.domain,
//               path: cookie.path,
//               secure: cookie.secure || false,
//               httpOnly: cookie.httpOnly || false,
//               expirationDate: cookie.expirationDate || 0,
//             },
//             function (cookie) {}
//           );
//         });
//       }
//       setTimeout(() => {
//         chrome.tabs.query(
//           { active: true, currentWindow: true },
//           function (tabs) {
//             chrome.tabs.sendMessage(tabs[0].id, {
//               message: "1",
//             });
//           }
//         );
//       }, 2500);
//     }, 500);
//   }
// });
